﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ZLCMS.Business;
using NHibernate;
using ZLCMS.Data.Model;
using NHibernate.Criterion;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;
using Newtonsoft.Json;

namespace ZLCMS.Web.SystemManage.Basics
{
    public partial class MealList : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        public string _name;
        public string _productNo;
        public string _productId;
        public string _status;
        public string _userAccount;

        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化全局变量值
            _name = Request["name"] == null ? "" : Request["name"];
            _productNo = Request["pno"] == null ? "" : Request["pno"];
            _productId = Request["pid"] == null ? "" : Request["pid"];
            _status = Request["status"] == null ? "" : Request["status"];
            _userAccount = UserKit.GetUserAccountFailLogin();

            //初始化DWZ分页控件
            _framePage = new FrameHelper(this) { RecordCount = GetListCount("restaurant") };

            //初始化控件值
            tb_search_name.Text = _name;
            tb_search_productno.Text = _productNo;

            if (!IsPostBack)
            {
                string opt = Request["opt"];
                //判断操作类型
                if (opt != null)
                {
                    ShowAndDropCallBack(opt);
                }
            }
           
            //绑定产品状态下拉框
            ParamKit.Bind("ProductStatus", ddl_status, null, new System.Collections.Generic.List<ListItem>() { ParamKit.ParamItemEx_QXZ() }, null, "-1", "-1");
            ddl_status.SelectedValue = _status;

            //填充网格数据
            BindList(_framePage.PageIndex, _framePage.PageSize);

        }

        #region 产品显示与下架操作 + private void ShowAndDropCallBack()
        /// <summary>
        /// 产品显示操作
        /// </summary>
        private void ShowAndDropCallBack(string opt)
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ShopProductInfo ProductData = session.Get<ShopProductInfo>(int.Parse(_productId));
                if (opt == "show")
                {
                    ProductData.Status = "0";
                }
                else if(opt=="drop")
                {
                    ProductData.Status = "1";
                }

                session.Save(ProductData);

                session.Flush();

                _framePage.Alert(new ReturnCode()
                {
                    statusCode = 200,
                    message = "操作成功!",
                    //callbackType = "closeCurrent",
                    navTabId = _framePage.NavTabId
                });
                //_framePage.AlertAndCloseCurrent("asdf");
            }
        }
        #endregion

        #region 填充网格数据 + protected void BindList(int pageIndex, int pageSize)
        /// <summary>
        /// 填充网格数据
        /// </summary>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        protected void BindList(int pageIndex, int pageSize)
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<ShopProductInfo>().Add(Expression.Eq("Kind", "restaurant"));
                SetupCondition(ref criteria);
                criteria.SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize);

                grid.DataSource = criteria.List();
                grid.DataBind();
            }
        }
        #endregion

        #region 填充查询条件 + private void SetupCondition(ref ICriteria criteria)
        /// <summary>
        /// 填充查询条件
        /// </summary>
        /// <param name="criteria"></param>
        private void SetupCondition(ref ICriteria criteria)
        {
            if (_name.Trim() != "")
            {
                criteria.Add(Expression.Like("ProductName", "%" + _name + "%"));
            }

            if (_productNo.Trim() != "")
            {
                criteria.Add(Expression.Like("ProductNo", "%" + _productNo + "%"));
            }

            if (_status.Trim() != "-1" && !string.IsNullOrEmpty(_status))
            {
                criteria.Add(Expression.Eq("Status", _status));
            }
            criteria.Add(Expression.Eq("OwnerAccount", _userAccount)).AddOrder(Order.Asc("ProductNo"));
        }
        #endregion

        #region 查询数据总数 + protected int GetListCount()
        /// <summary>
        /// 查询数据总数
        /// </summary>
        /// <returns></returns>
        protected int GetListCount(string BusinessKind)
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<ShopProductInfo>().Add(Expression.Eq("Kind", BusinessKind));
                SetupCondition(ref criteria);
                object obj = criteria.SetProjection(Projections.RowCount()).UniqueResult();
                return int.Parse(obj.ToString());
            }
        }
        #endregion

        #region 显示操作项目链接 -protected string ShowLink(string ids, string status)
        /// <summary>
        /// 显示操作项目链接
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        protected string ShowLink(string pid, string status)
        {
            string retString = null;

            if (status == "0")
            {
                retString = "<a href='/SystemManage/Basics/MealInfo/MealList.aspx?pid=" + pid + "&opt=drop' target='ajaxTodo' title='请确定要下架吗?'>下架</a>&nbsp;&nbsp;";
            }
            else if (status == "1")
            {
                retString = "<a href='/SystemManage/Basics/MealInfo/MealList.aspx?pid=" + pid + "&opt=show' target='ajaxTodo' title='请确定要显示吗?'>显示</a>&nbsp;&nbsp;";
            }

            return retString;
        }
        #endregion


    }
}
